﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Xml;
using Sogeti.QueryWrapper;
using Sogeti.VSExtention;
using System.Windows;

namespace Mskold.IterationManager
{
    class ChangeIterationViewModel
    {
        public void Load(TeamExplorerIntergator te)
        {

            teamExplorer = te;
            Config = new IterationManagerConfig(teamExplorer);
            Config.Load();

            Actions = new List<IAction>();

            if (Config.ConfigStore.ChangeCurrentIteratonFolder)
            {
                Sogeti.QueryWrapper.CommonStructureWrapper csw = new Sogeti.QueryWrapper.CommonStructureWrapper(teamExplorer.tpCollection);
                IterationsElement = csw.GetNodesTree(Sogeti.QueryWrapper.StructureType.Iteration, teamExplorer.tpName);

                TPQueryWrapper queryWrp = new TPQueryWrapper(teamExplorer);
                currentFolder = queryWrp.FindWiqlFolder(Config.ConfigStore.CurrentIterationWIQLFolder);
                string usedIteration = queryWrp.GetUsedIterationsInFolder(currentFolder).First();

                CurrentIteration = new KeyValuePair<string, string>(usedIteration, @"\Iteration\" + usedIteration);

                Actions.Add(new ReplaceIterationInFolderAction("Edit queris in query folder "+ currentFolder.Name));
            }

        }

        public IEnumerable<string> ActionTitles { 
            get {
                if (Actions != null)
                {
                    return from a in Actions select a.Title;
                }
                else
                    return new List<string>(); ;
            }
        }
        
        public void cmdChangeIteration(Window parentWindow)
        {


            Dictionary<string, object> sharedVariables = new Dictionary<string, object>();
            sharedVariables.Add("FromIterationPath", CurrentIteration.Key);
            sharedVariables.Add("ToIterationPath", ChangeToIteration);
            sharedVariables.Add("TeamExplorer", teamExplorer);
            sharedVariables.Add("CurrentIterationFolder", Config.ConfigStore.CurrentIterationWIQLFolder);

            


            ProgressDlg progress = new ProgressDlg();
            progress.DoProgress(parentWindow, "Change Current Iteration", sharedVariables, Actions);

        }

        public Dictionary<string, string> Iterations
        {

            get
            {
                String s=@"\Iteration\";
                
                Dictionary<string, string> lst= new Dictionary<string, string>();
                if (IterationsElement != null)
                {
                    foreach (XmlNode x in IterationsElement.SelectNodes(@"//Node"))
                    {
                        lst.Add(x.Attributes["Path"].Value.ToString(), ReplaceFirst(x.Attributes["Path"].Value.ToString(), s, ""));

                    }
                }
                return lst;
            }
        }

        public string ReplaceFirst(string org, string search, string replace)
        {
            if (org.Contains(search))
            {
                int endOfSearch = org.IndexOf(search) + search.Length;
                string s = org.Substring(0, org.IndexOf(search) + search.Length);
                if(org.Length> endOfSearch+1)
                s = s.Replace(search, replace);
                string remaining = "";
                if (org.Length > endOfSearch )
                {
                    remaining = org.Substring(endOfSearch);
                }
                    
                
                org=s + remaining;
            }
            return org;

        }

        public XmlElement IterationsElement
        {
            get { return _xml; }
            set { _xml = value; }

        }

        public IterationManagerConfig Config
        {
            get { return _config; }
            set { _config = value; }
        }
        public KeyValuePair<string, string> CurrentIteration { get; set; }

        public string CurrentIterationPath
        {
            get
            {
                string s=  CurrentIteration.Key;
                if(s!=null)
                {
                    s=s.Substring(s.IndexOf(@"\", 2));
                }
                
                return s;
            }
        }

        public string ChangeToIteration { get; set; }

        private IterationManagerConfig _config;
        private TeamExplorerIntergator teamExplorer;
        private XmlElement _xml;
        private WiqlFolder currentFolder;


        public List<IAction> Actions { get; set; }
       
    }
}
